<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Virtual Joystick</title>
  <link rel="manifest" href="manifest.json" />
  <meta name="theme-color" content="peachpuff" />
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      background-color: #f0f0f0;
      font-family: Arial, sans-serif;
      touch-action: none;
      overflow: hidden;
    }

    .div-body {
      width: 100%;
      max-width: 970px;
      height: 100vh;
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      position: relative;
    }

    .panel {
      flex: 1;
      padding: 20px;
      display: flex;
      flex-direction: column;
    }

    .left-panel {
      background-color: rgba(0, 255, 255, 0.2);
    }

    .right-panel {
      background-color: rgba(127, 255, 212, 0.2);
    }

    .output {
      background-color: rgba(255, 255, 255, 0.8);
      padding: 15px;
      border-radius: 10px;
      margin-bottom: 20px;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    .joystick-container {
      position: absolute;
      bottom: 25%;
      left: 25%;
      transform: translateX(-50%);
      width: 150px;
      height: 150px;
    }

    .joystick-base {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, 0.2);
      position: relative;
      box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.2);
    }

    .joystick-thumb {
      width: 60px;
      height: 60px;
      border-radius: 50%;
      background-color: rgba(255, 0, 0, 0.7);
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      cursor: pointer;
      touch-action: none;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
      transition: transform 0.1s ease;
    }

    /* 竖屏样式 */
    @media (max-width: 768px) and (orientation: portrait) {
      .div-body {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
      }

      .joystick-container {
        position: relative;
        bottom: auto;
        left: auto;
        transform: none;
        margin: 30px auto;
      }

      .joystick-container {
        width: auto;
        height: auto;
      }

      .joystick-thumb {
        width: auto;
        height: auto;
      }
    }

    /* 横屏样式 */
    @media (max-width: 768px) and (orientation: landscape) {
      .div-body {
        flex-direction: row;
        height: 100vh;
      }

      .panel {
        padding: 10px;
      }

      .joystick-container {
        position: fixed;
        right: 20px;
        left: auto;
        bottom: 50px;
        transform: none;
        width: auto;
        height: auto;
      }

      .joystick-thumb {
        width: auto;
        height: auto;
      }

      .output {
        padding: 10px;
        font-size: 14px;
      }
    }

    .slider-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      height: 300px;
    }

    .slider-value {
      margin-bottom: 20px;
      font-size: 24px;
      font-weight: bold;
    }

    .slider-track {
      width: 8px;
      height: 300px;
      background-color: #ddd;
      border-radius: 4px;
      position: relative;
      cursor: pointer;
    }

    .slider-thumb {
      width: 30px;
      height: 30px;
      background-color: #4CAF50;

      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      cursor: grab;
      touch-action: none;
      /* 防止触摸设备上的默认行为 */
    }

    .slider-fill {
      position: absolute;
      bottom: 0;
      width: 100%;
      background-color: #4CAF50;
      border-radius: 4px;
    }
  </style>
</head>

<body>
  <div class="div-body">
    <div class="panel left-panel">
      <div class="output">
        <h3>左摇杆数据</h3>
        <p>X: <span id="x-value">0</span></p>
        <p>Y: <span id="y-value">0</span></p>
        <p>角度: <span id="angle-value">0</span>°</p>
      </div>
      <div class="joystick-container">
        <div class="joystick-base" id="joystick-base">
          <div class="joystick-thumb" id="joystick-thumb"></div>
        </div>
      </div>
    </div>

    <div class="panel right-panel">
      <!-- 右侧面板内容 -->
      <div class="slider-container">
        <div class="output">
          <h3>右摇杆数据</h3>
          <p>Y: <span id="sliderValue">0</span></p>
        </div>
        <div class="slider-track" id="sliderTrack">
          <div class="slider-thumb" id="sliderThumb"></div>
          <div class="slider-fill" id="sliderFill"></div>
        </div>
      </div>
    </div>
  </div>

</body>

</html>
<script>
  const thumb = document.getElementById('joystick-thumb');
  const base = document.getElementById('joystick-base');
  const xValue = document.getElementById('x-value');
  const yValue = document.getElementById('y-value');
  const angleValue = document.getElementById('angle-value');

  // 如果是移动设备，检查方向
  if (/Mobi|Android/i.test(navigator.userAgent)) {
    checkOrientation();
    window.addEventListener('orientationchange', checkOrientation);
  }
  // 强制横屏提示（可选）
  function checkOrientation() {
    if (window.innerHeight > window.innerWidth) {
      // 竖屏提示
      const msg = document.createElement('div');
      msg.innerHTML = '请将设备横屏以获得最佳体验';
      msg.style.position = 'fixed';
      msg.style.top = '0';
      msg.style.left = '0';
      msg.style.width = '100%';
      msg.style.height = '100%';
      msg.style.backgroundColor = 'rgba(0,0,0,0.8)';
      msg.style.color = 'white';
      msg.style.display = 'flex';
      msg.style.justifyContent = 'center';
      msg.style.alignItems = 'center';
      msg.style.zIndex = '9999';
      msg.style.fontSize = '24px';
      msg.style.textAlign = 'center';
      document.body.appendChild(msg);

      // 监听横屏变化
      const checkAgain = function () {
        if (window.innerHeight < window.innerWidth) {
          document.body.removeChild(msg);
          window.removeEventListener('resize', checkAgain);
        }
      };
      window.addEventListener('resize', checkAgain);
    }
  }
</script>
<script src="./websocket.js"></script>
<script src="./joystick.js"></script>
<script src="./slider.js"></script>